<?php 

//Luu giu trang thai loc
$v_receive_date_filter = "";
if(isset($_REQUEST['hdn_received_date_filter'])){
	$v_receive_date_filter = $_REQUEST['hdn_received_date_filter'];
} 
$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
if(isset($_REQUEST['hdn_recordtype_filter'])){
	$v_recordtype_filter = $_REQUEST['hdn_recordtype_filter'];
} 
//echo $v_recordtype_filter."---";exit;
$v_record_id = 0;
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id = intval($_REQUEST['hdn_record_id']);
}
$v_record_id_list = "";
if(isset($_REQUEST['hdn_record_id_list'])){
	$v_record_id_list = $_REQUEST['hdn_record_id_list'];
	$v_arrs_record_id=explode(",",$v_record_id_list);
}

if(isset($_REQUEST['hdn_record_number_page'])&&$_REQUEST['hdn_record_number_page']!=""){
	$v_limit_row = $_REQUEST['hdn_record_number_page'];
} 

//DatCQ 14102008 Phan cong thu ly tai tiep nhan.
$v_handler_id = "";
if(isset($_REQUEST['hdn_handler'])){
	$v_handler_id = $_REQUEST['hdn_handler'];
	//echo $v_handler_id;
}
//End 14102008 Phan cong thu ly tai tiep nhan.

$v_current_status = $arr_record_status[2][0];
$v_status_of_handover = 1;
$v_staff_id = $_SESSION['staff_id'];

if(_is_sqlserver()){
	//CHUYEN_HS_TP_LDPD:chuyen thang len Can bo phe duyet
	//TRINH_LDLC_TP:trinh lanh dao phe duyet lan cuoi
	$sql_single_config="Exec Onegate_ConfigRecordtypeGetSingle ";
	$sql_single_config=$sql_single_config."'".$v_recordtype_filter."'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_single_config = $ado_conn->GetRow($sql_single_config); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	$v_c_system_conf=$arr_single_config['C_SYSTEM_CONFIG'];
	$v_dateProcess=getColumnRecordtypeSingle($v_recordtype_filter,'C_PROCESS_DATE');

//------------------------
	//Leav_begin_update:14/06/2008 
	//echo $v_worktype;
	
	//Leav_begin_update:11112008:
	//Tong so ngay thu ly cho ho so nay:A
	//echo "test:".$recordtype_single['C_PROCESS_DATE'];exit;
	if($v_worktype=="NOP_THUE"){
			if(is_contain_config($v_c_system_conf,'CHUYEN_HS_TP_LDPD')){//truong hop nay su dung doi voi 027,028
				$arrs_id_record2=array(sizeof($v_arrs_record_id));
				for($i=0;$i<sizeof($v_arrs_record_id);$i++){
					$v_record_id_i=$v_arrs_record_id[$i];
					$sql_fk_leader_id="SELECT FK_HANDLE_LEADER_FIRST_TIME
										FROM T_ONEGATE_RECORD 
										WHERE pk_record=".$v_record_id_i;
					$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
					$rs_leader = $ado_conn->GetRow($sql_fk_leader_id); 
					$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
					$_leader=$rs_leader['FK_HANDLE_LEADER_FIRST_TIME'];
					$arrs_id_record2[$i]=TRINH_LD_PHONG_LAN_DAU;
				}
								////////////////////////
				$kq_tl = _TRINH_KY;
				$is = false;
				//kiem tra phong ban
				$arr_staff_appro = _get_item_attr_by_id($_SESSION['arr_all_staff'],$_leader, 'unit_id' );
				$_arr_unit_code =  explode(",",$kq_tl); //ham cat chuoi luu vao mang $_arr_unit_code 
				// var_dump ($_arr_unit_code);
				$j = 0;	
				$arr_unit_id = array();			
				foreach($_SESSION['arr_all_unit'] as $v_unit)
				{
					for($i = 0 ; $i < sizeof($_arr_unit_code) ; $i ++)
					{
						if($v_unit['code'] == $_arr_unit_code[$i])
						{
							$arr_unit_id[$j] = $v_unit['id'] ;
							$j ++;
						}						
					}
				}  
				$v_size_unit = sizeof($arr_unit_id);
				$is = false;
				if ($v_size_unit >=1)
				{
					for ($i = 0; $i <$v_size_unit && $is == false ; $i ++)
					{
						if($arr_staff_appro == $arr_unit_id[$i])
							 $is = true;
					}
				}
				$v_work = 'TRINH_LD_PHONG_LAN_DAU';
				if ($is == true)
				{
					if ( $v_work == 'TRINH_LD_PHONG_LAN_DAU') {
						$v_work = 'TRINH_LD_VP_LAN_DAU';
					}
				}
				//////////////////////////////////////
				$sql = "Exec Onegate_RecordWorkSystemUpdate_NieuVV " ;
				$sql = $sql . "'" . $v_record_id_list . "'";
				$sql = $sql . "," . $v_staff_id;
				$sql = $sql . ",'".$v_work."'";
				$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_work_date) . "'" ;
				$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
				$sql = $sql . ",'" . $v_reason . "'" ;
					if(($v_status_refuse == 1) && ($v_reason<>"")){
						$sql = $sql . "," . $v_status_refuse;
					}else{
						$v_status_refuse = 0;
						$sql = $sql . "," . $v_status_refuse;
					}
					$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
					$rs = $ado_conn->GetRow($sql); 
					$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
					
				for($i=0;$i<sizeof($v_arrs_record_id);$i++){
						$_leader_i=	$arrs_id_record2[$i];
						$v_record_id_i=$v_arrs_record_id[$i];
						$sql2="	update 	T_ONEGATE_RECORD 
								 set 		FK_HANDLE_LEADER_FIRST_TIME=".$_leader." "; 
						$sql2=$sql2." where 	pk_record=".$v_record_id_i;
						
						$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
						$rs = $ado_conn->GetRow($sql2); 
						$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
				}
					
		}else{
				$sql = "Exec Onegate_RecordHandoverUpdate_Ban_giao_thue ";
				$sql = $sql . " '" .  $v_record_id_list . "'";
				$sql = $sql . "," . $v_staff_id ;
				$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
				$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetRow($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
		}
			//echo $sql;exit;
	}elseif($v_worktype=="NOP_PHAT")
	{
			if(is_contain_config($v_c_system_conf,'CHUYEN_HS_TP_LDPD')){
				$arrs_id_record2=array(sizeof($v_arrs_record_id));
					for($i=0;$i<sizeof($v_arrs_record_id);$i++){
						$v_record_id_i=$v_arrs_record_id[$i];
						$sql_fk_leader_id="SELECT FK_HANDLE_LEADER_FIRST_TIME
											FROM T_ONEGATE_RECORD 
											WHERE pk_record=".$v_record_id_i;
						$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
						$rs_leader = $ado_conn->GetRow($sql_fk_leader_id); 
						$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
						$_leader=$rs_leader['FK_HANDLE_LEADER_FIRST_TIME'];
						$arrs_id_record2[$i]=$_leader;
				}
				////////////////////////
				$kq_tl = _TRINH_KY;
				$is = false;
				//kiem tra phong ban
				$arr_staff_appro = _get_item_attr_by_id($_SESSION['arr_all_staff'],$_leader, 'unit_id' );
				$_arr_unit_code =  explode(",",$kq_tl); //ham cat chuoi luu vao mang $_arr_unit_code 
				// var_dump ($_arr_unit_code);
				$j = 0;	
				$arr_unit_id = array();			
				foreach($_SESSION['arr_all_unit'] as $v_unit)
				{
					for($i = 0 ; $i < sizeof($_arr_unit_code) ; $i ++)
					{
						if($v_unit['code'] == $_arr_unit_code[$i])
						{
							$arr_unit_id[$j] = $v_unit['id'] ;
							$j ++;
						}						
					}
				}  
				$v_size_unit = sizeof($arr_unit_id);
				$is = false;
				if ($v_size_unit >=1)
				{
					for ($i = 0; $i <$v_size_unit && $is == false ; $i ++)
					{
						if($arr_staff_appro == $arr_unit_id[$i])
							 $is = true;
					}
				}
				$v_work = 'TRINH_LD_PHONG_LAN_DAU';
				if ($is == true)
				{
					if ( $v_work == 'TRINH_LD_PHONG_LAN_DAU') {
						$v_work = 'TRINH_LD_VP_LAN_DAU';
					}
				}
				//////////////////////////////////////
				$sql = "Exec Onegate_RecordWorkSystemUpdate_NieuVV " ;
				$sql = $sql . "'" . $v_record_id_list . "'";
				$sql = $sql . "," . $v_staff_id;
				$sql = $sql . ",'".$v_work."'";
				$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_work_date) . "'" ;
				$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
				$sql = $sql . ",'" . $v_reason . "'" ;
					if(($v_status_refuse == 1) && ($v_reason<>"")){
						$sql = $sql . "," . $v_status_refuse;
					}else{
						$v_status_refuse = 0;
						$sql = $sql . "," . $v_status_refuse;
					}
					$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
					$rs = $ado_conn->GetRow($sql); 
					$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
					
				for($i=0;$i<sizeof($v_arrs_record_id);$i++){
						$_leader_i=	$arrs_id_record2[$i];
						$v_record_id_i=$v_arrs_record_id[$i];
						$sql2="	update 	T_ONEGATE_RECORD 
								 set 		FK_HANDLE_LEADER_FIRST_TIME=".$_leader." "; 
						$sql2=$sql2." where 	pk_record=".$v_record_id_i;
						
						$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
						$rs = $ado_conn->GetRow($sql2); 
						$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
				}
			}else{
				$sql = "Exec Onegate_RecordHandoverUpdate_Ban_giao_phat ";
				$sql = $sql . " '" .  $v_record_id_list . "'";
				$sql = $sql . "," . $v_staff_id ;
				$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
				$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetRow($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
			}
	}else{
			$sql = "Exec Onegate_RecordHandoverUpdate ";
			$sql = $sql . " '" .  $v_record_id_list . "'";
			$sql = $sql . "," . $v_staff_id ;
			$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
			$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
			$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
			echo $sql.'<br>'; 
			$rs = $ado_conn->GetRow($sql); 
			$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
			echo $v_error;
			//DatCQ 14102008 Phan cong thu ly tai tiep nhan.
			if (is_null($v_error) || $v_error == "")
			{
				if($v_handler_id!= 0 && $v_handler_id!=''){
					$sql = "Exec Onegate_RecordHandlerUpdate ";
					$sql = $sql . " '" .  $v_record_id_list . "'";
					$sql = $sql . "," . $v_staff_id ;
					$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
					$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
					$sql = $sql . "," . $v_handler_id ;
					$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
					$rs = $ado_conn->GetRow($sql); 
				//echo $sql.'<br>'; 
				}
			}
			//exit;
			//End 14102008 Phan cong thu ly tai tiep nhan.			
	}//Leav_end_update:14/06/2008 
	if (is_null($v_error) || $v_error == "")
	{
		if($v_worktype=="NOP_THUE" || $v_worktype=="NOP_PHAT"){
				$v_ldpd='LD_PD_THUE';
				if($v_worktype=="NOP_PHAT"){
					$v_ldpd='LD_PD_PHAT';
				}
				$v_dateEnd = _ddmmyyyy_to_yyyymmdd($v_receive_date_filter);
				$sql="exec Onegate_setAppoitedDateAfterGetCertificateOfTaxOrPunish";
				$sql = $sql . "'" . $v_record_id_list. "'";
				$sql = $sql . ",'" . $v_ldpd. "'";
				$sql = $sql . ",'" . $v_dateEnd. "'";
				$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
				$rs = $ado_conn->GetRow($sql); 
				$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
		}
	}
}
sleep(0);
if (!is_null($v_error) && $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_goto_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}
?>
<form action="index.php" method="post" name="f_back">
	
	<?php //Leav_begin_update:14/06/2008 
	if($v_worktype=="NOP_THUE"){?>
	<input type="hidden" name="fuseaction" value="DISPLAY_ALL_RECORD_DA_NOP_THUE">
	<?php }elseif($v_worktype=="NOP_PHAT"){?>
	<input type="hidden" name="fuseaction" value="DISPLAY_ALL_RECORD_DA_NOP_PHAT">
	<?php }else{?>
	<input type="hidden" name="fuseaction" value="DISPLAY_ALL_RECORD">	
	<?php }//Leav_end_update:14/06/2008?>
	<input type="hidden" name="hdn_record_id" value="<?php echo $v_record_id;?>">
	<input type="hidden" name="hdn_recordtype_filter" value="<?php echo $v_recordtype_filter; ?>">
	<input type="hidden" name="hdn_received_date_filter" value="<?php echo $v_receive_date_filter; ?>">
	<input type="hidden" name="hdn_current_item_id" value="<?php echo $v_record_id;?>">
	<input type='hidden' name='hdn_record_number_page' value='<?php echo $v_limit_row;?>'>
</form>
<Script language="javascript">
	document.forms(0).submit();
</Script>